package com.escape.subject.application.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * @Author: escape_zebra
 * @CreateTime: 2024-12-27  20:24
 * @Description: TODO
 * @Version: 1.0
 */
@Configuration
public class GlobalConfig extends WebMvcConfigurationSupport{

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(mappingJackson2HttpMessageConverter());
    }


    /**
     * 自定义 mappingJackson2HttpMessageConverter
     * 实现：空值忽略，空字段可以返回
     * @return
     */
    private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false); // 空字段可以返回
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); // 空值忽略
        return new MappingJackson2HttpMessageConverter(objectMapper);
    }

}
